package com.example.timetable.method;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.widget.Toast;

import com.example.timetable.AlertActivity;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AlertManage {
    private SharedPreferences preferences;
    //初始化提醒框
    public void initAlert(Context context, String flag){
        preferences = context.getSharedPreferences("GoToClassAlert", Context.MODE_PRIVATE);
        boolean alert=preferences.getBoolean("alert",false);
        //long time=preferences.getLong("time",0);
        long time=getNextTime(context);
        int selectMinute=preferences.getInt("minute",1);
        if(alert&&time!=0){
            Intent intent=new Intent(context, AlertActivity.class);
            PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,0);
            AlarmManager alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.setExact(AlarmManager.RTC_WAKEUP,time-selectMinute*60*1000,pendingIntent);
            if(flag=="show")
                Toast.makeText(context,"闹钟设置成功，课前"+selectMinute+"分钟提醒",Toast.LENGTH_LONG).show();
        }
    }
    //删除闹钟
    public void deleteAlert(Context context){
        Intent intent=new Intent(context,AlertActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,0);
        AlarmManager alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
    }

    //获取下节课的时间
    private long getNextTime(Context context){
        Database db=new Database(context);
        Calendar c=Calendar.getInstance();
        int week=c.get(Calendar.DAY_OF_WEEK)-2;
        int hourNow=c.get(Calendar.HOUR_OF_DAY);
        int minuteNow=c.get(Calendar.MINUTE);
        if(week==-1) week=6; //周末
        for(int i=week;i<7;i++){ //从当前星期找到本周最后一天
            Cursor cursor=db.select(i);
            if(cursor.getCount()>0){
                while(cursor.moveToNext()){
                    String time=cursor.getString(6);
                    String [] t=time.split("\\.",3);
                    int hour=Integer.parseInt(t[0]);
                    int minute=Integer.parseInt(t[1]);
                    if(i>week||hour>hourNow||(hour==hourNow&&minute>minuteNow)){
                        try{
                            Date date=new Date();
                            DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
                            date =dateFormat.parse(dateFormat.format(date));
                            int subTime=i-week;
                            long lt=date.getTime()+subTime*24*60*60*1000+hour*60*60*1000+minute*60*1000;
                            int selectMinute=preferences.getInt("minute",1);
                            long startTime=lt-selectMinute*60*1000;
                            Date date1=new Date();
                            if(startTime<date1.getTime()){
                                continue;
                            }
                            return lt;
                        }catch (Exception e){
                            System.out.println(e);
                        }
                    }
                }
            }
        }
        for(int i=0;i<week;i++){ //当前周未找到课程，在下周找
            Cursor cursor=db.select(i);
            if(cursor.getCount()>0){
                while(cursor.moveToNext()){
                    String time=cursor.getString(6);
                    String [] t=time.split("\\.",3);
                    int hour=Integer.parseInt(t[0]);
                    int minute=Integer.parseInt(t[1]);
                    if(i>week||hour>minuteNow||(hour==hourNow&&minute>minuteNow)){
                        try{
                            Date date=new Date();
                            DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
                            date =dateFormat.parse(dateFormat.format(date));
                            int subTime=i+7-week;
                            long lt=date.getTime()+subTime*24*60*60*1000+hour*60*60*1000+minute*60*1000;
                            return lt;
                        }catch (Exception e){
                            System.out.println(e);
                        }
                    }
                }
            }
        }

        return 0L;
    }

}
